import pick from 'lodash.pick'
import Vue from 'vue'
import { USER_DATA } from "@/store/mutation-types"
import { postAction, getAction, downFile, httpAction } from '@/api/manage'
import store from '@/store'
import debounce from 'lodash/debounce';
const keyDownMinxin = {
  data() {
    return {
      globalInd:-1,
      kuIndex:0,
      iskeyEnterNum:0,
      asyncKey:false,
      totalKey:0
    }
  },
  // activated(){
  //   console.log(window.keyboardMethodRegistered,'window.keyboardMethodRegistered--activated')
  //   if (!window.keyboardMethodRegistered) {
  //     window.addEventListener('keyup',this.handleFormKeyPress);
  //     window.keyboardMethodRegistered = true;
  //   }
  // },
  mounted() {
    console.log(window.keyboardMethodRegistered,'window.keyboardMethodRegistered-mounted')
    if (!window.keyboardMethodRegistered) {
      window.addEventListener('keyup',this.handleFormKeyPress);
      window.keyboardMethodRegistered = true;
    }
    var that = this;
    this.$nextTick(() => {
      $('.tabkey-box').on('click', function() {  
        var target = $('.tabkey-box .ant-col input:enabled:not(.ant-select-search__field)').add('.tabkey-box .ant-col .ant-select:not(.ant-select-disabled)') // 获取当前行的 输入框元素
        target.each(function(i,e) {
          if($(this).hasClass('ant-calendar-picker-input')){
            $(this).click(function(){
              var index = target.index(this); 
              // that.globalInd = index;
              that.$store.commit('SET_GLOBALVUEXIND',index);
              console.log('Clicked div index:', index);
            })
            
          }
          
        });
      
      }); 
    })
  },
  watch: {
    // 监听路由对象 $route 的变化
    $route(to, from){
        console.log('路由变化了')
        console.log('当前页面路由：' + to);
        console.log('上一个路由：' + from);
        this.globalInd = 0
    },
  },
  beforeDestroy(){
    console.log('distroy')
    window.keyboardMethodRegistered = false
    window.removeEventListener('keyup',this.handleFormKeyPress);
  },
  methods: {
    selectText(element) {
        if(!element){
          return
        }
        if (element.setSelectionRange) {
            element.setSelectionRange(0, element.value.length);
        } else if (element.createTextRange) {
            var range = element.createTextRange();
            range.collapse(true);
            range.select();
        }
    },
    getRealIndex(){
      const that = this;
      var target = $('.tabkey-box .ant-col input:enabled:not(.ant-select-search__field),.tabkey-box .ant-col .texareafocus textarea:enabled').add('.tabkey-box .ant-col .ant-select:not(.ant-select-disabled)').add('.tabkey-box .ant-col .ant-calendar-picker .ant-calendar-picker-input') // 获取当前行的 输入框元素

      setTimeout(() => {
          
        target.each(function(i,e){
          const $this = $(this);
          if($this.attr('placeholder') && $this.attr('placeholder').indexOf('时间')>-1){
            return
          }
          console.log('11111111+++++++')
          if($this.is(':focus') || $this.hasClass('ant-select-focused')){ // 如果当前输入框是 focus状态 执行操作
            
            that.globalInd = target.index(this)
          }
        })
        
      },200)
    },
    handleFormKeyPress:debounce(function(e){
      var that = this;
      var ind = 0;
      // 
      var target = $('.tabkey-box .ant-col input:enabled:not(.ant-select-search__field),.tabkey-box .ant-col .texareafocus textarea:enabled').add('.tabkey-box .ant-col .ant-select:not(.ant-select-disabled)').add('.tabkey-box .ant-col .ant-calendar-picker .ant-calendar-picker-input') // 获取当前行的 输入框元素

      
      // 入库供应商 的下拉搜索处理
      const listItems = $('.vxe-pulldown--panel.animat--enter').find('.list-item1');
      const listLength = listItems.length;
      if (e.keyCode === 40 || e.keyCode === 38) {
        // 处理上下键逻辑
        this.kuIndex += e.keyCode === 40 ? 1 : -1;
        // 循环索引
        if (this.kuIndex < 0) {
          this.kuIndex = listLength - 1;
        } else if (this.kuIndex > listLength - 1) {
          this.kuIndex = 0;
        }
        // 更新样式
        listItems.eq(this.kuIndex).addClass('cur').siblings().removeClass('cur');
      }
      // end
      
      if(e.keyCode == 16){
        console.log(e,'e.keyCode')
        if(e.keyCode == 13){
         
        }
      }
      if(e.keyCode == 13){
        that.iskeyEnterNum ++  // 切换下一个的标识字段 
        console.log(target,that.iskeyEnterNum,'000000000+++++++')
        setTimeout(() => {
          
          target.each(function(i,e){
            const $this = $(this);
            if($this.attr('placeholder') && $this.attr('placeholder').indexOf('时间')>-1){
              return
            }
            console.log('11111111+++++++')
            if($this.is(':focus') || $this.hasClass('ant-select-focused')){ // 如果当前输入框是 focus状态 执行操作
              
              // ind = i
              // that.globalInd = ind
              // console.log($this,that.globalInd,'9999999+++++++')
              //下拉框
              if($this.hasClass('ant-select')){
                if($this.find('.ant-select-selection-selected-value').length<1){
              
                  $this.click()
                  that.iskeyEnterNum = 0
                  return
                }else{
                  if(that.iskeyEnterNum>0 ){
                    that.setNextFocusDom(target)
                  }
                }
              }
            
             
              //输入框
              if(that.iskeyEnterNum >0){
                const placeholder = $this.attr('placeholder');
                if($this.val()){
                  if(placeholder){
                      if($this.attr('placeholder').indexOf('可搜索的下拉框')>-1){
                        $('.vxe-pulldown--panel.animat--enter').find('.list-item1').eq(that.kuIndex).click();
                        that.setNextFocusDom(target)
                        return
                      }
                      if($this.attr('placeholder').indexOf('检索')<0){
                        that.setNextFocusDom(target)
                      }
                  }
                }else{
                  // 搜索弹窗的 不算
                  if(!$this.parent().hasClass('novalenter')){
                      // 非必填项目  回车 可以为空
                    if($this.parents('.ant-form-item').find('.ant-form-item-required').length<1){
                      that.setNextFocusDom(target)
                    }
                  }
                 
                }
              }
            }
          })
          
        },200)
      }else{
          //除了回车 ，其他的按键 都重置 状态
          that.iskeyEnterNum  = 0
        
      }

    },50),
    setTimeNext(index){
      var that = this
      const target = $('.tabkey-box .ant-col input:enabled:not(.ant-select-search__field)').add('.tabkey-box .ant-col .ant-select:not(.ant-select-disabled)').add('.tabkey-box .ant-col .ant-calendar-picker .ant-calendar-picker-input') // 获取当前行的 输入框元素
      this.globalInd = index
      setTimeout(function(){
        that.setNextFocusDom()
      },50)
    },
    resetKeydown(){
      this.iskeyEnterNum = 0
      this.globalInd = -1
      console.log('setTimeNext')
      this.setTimeNext(-1)
    },
    setNextFocusDom:debounce(function(targets){
      const that = this
      console.log(that.globalInd,'that.globalInd')
      setTimeout(() => {
          const target = targets?targets:$('.tabkey-box .ant-col input:enabled:not(.ant-select-search__field),.tabkey-box .ant-col .texareafocus textarea:enabled').add('.tabkey-box .ant-col .ant-select:not(.ant-select-disabled)').add('.tabkey-box .ant-col .ant-calendar-picker .ant-calendar-picker-input') // 获取当前行的 输入框元素
          that.totalKey = target.length
          
          target.each(function(i,e){
            var $this = $(this)
            if($this.is(':focus') || $this.hasClass('ant-select-focused')){ 
              that.globalInd = target.index(this)
            }
          })
          if(target.hasClass('ant-select')){
            target.removeClass('ant-select-focused')
          }else{
          
            target.blur()   
          }
         
          var realIndex = that.globalInd+1
          
          console.log(target.eq(realIndex),that.globalInd,'that.globalInd')
          if(target.eq(realIndex).hasClass('ant-select')){
            console.log('that.globalInd5555')
            target.eq(realIndex).addClass('ant-select-focused')
            target.eq(realIndex).click()
          }else{
            console.log(that.globalInd,'that.globalInd6666')
            target.eq(realIndex).focus()
            that.selectText(target.eq(realIndex)[0])
            // 日历开启弹窗
            if(target.eq(realIndex).hasClass('ant-calendar-picker-input')){
              target.eq(realIndex).click()
            }
            
          }

          that.iskeyEnterNum = 0
       
       
      },50)
    },50)
  }
  
 
}

export { keyDownMinxin }